package com.wjm.serversocket.test1;

import java.io.*;
import java.net.Socket;
import java.text.NumberFormat;
/**
 * Socket java程序间 上传文件，尝试使用多线程，失败不是这样用的
 */
public class MyRunnable implements Runnable {
    private int start;
    private int end;

    public MyRunnable(int start, int end) {
        this.start = start;
        this.end = end;
        System.out.println(Thread.currentThread().getName() + "MyRunnable =》start" + start + "end" + end);
    }

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() + "run start");

        try {

            uploadVedio();

        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + "run end");
    }

    private void uploadVedio() throws Exception {

        Socket socket = new Socket(UploadPicServer.ip, UploadPicServer.port);
        if (socket == null) {
            System.out.println("超时");
            return;
        }
        long startCurrentTimeMillis = System.currentTimeMillis();

        File file = new File(UploadPicServer.fileStr);
        float length = file.length();
//        System.out.println("length==>" + length);

        String absolutePath = file.getAbsolutePath();
        String filename = absolutePath.substring(absolutePath.lastIndexOf(File.separator) + 1, absolutePath.length());
//        System.out.println("文件路径==>" + absolutePath);//absolutePath==>i:\a_100mb.mp4
//        System.out.println("客户端文件名==>" + filename);//absolutePath==>i:\a_100mb.mp4

        FileInputStream fis = new FileInputStream(UploadPicServer.fileStr);

        OutputStream socketOut = socket.getOutputStream();
//        new RandomAccessFileTest(new )
//123,456,789
        int len;
        byte[] fisBuf = new byte[1024];
        float lenCount = 0;//用于计算上传进度
        NumberFormat numberFormat = NumberFormat.getInstance();
        numberFormat.setMaximumFractionDigits(0); // 设置精确到小数点后2位
        String lastResult = null;
        while ((len = fis.read(fisBuf,start,end)) != -1) {
            lenCount += len;
            float bfbD = lenCount / length * 100;
            String result = numberFormat.format(bfbD);
            if (!result.equals(lastResult)) {
                if ("0".equals(result) || "10".equals(result) || "50".equals(result) || "90".equals(result) || "100".equals(result))
//                if("0".equals(result)||"10".equals(result)||"20".equals(result)||"30".equals(result)||"40".equals(result)||"50".equals(result)||"60".equals(result)||"70".equals(result)||"80".equals(result)||"90".equals(result)||"100".equals(result))
                    System.out.println(Thread.currentThread().getName() + "上传进度：" + result + "%");//
            }
            lastResult = result;

            socketOut.write(fisBuf, start, len);
//            socketOut.write(fisBuf, 0, len);
        }
        socket.shutdownOutput();//结束标记

        InputStream socketIs = socket.getInputStream();
//            byte[] socketIsBuf = new byte[128];
        int socketIsLen;
        if ((socketIsLen = socketIs.read()) == -1) {//上传完成
            long end = System.currentTimeMillis();
            long time = end - startCurrentTimeMillis;
            String timeStr = time / 1000 + "s";
//            System.out.println("上传完成");
            System.out.println(Thread.currentThread().getName() + "上传用时" + timeStr);
        } else {//没上传完
            System.out.println(Thread.currentThread().getName() + "没上传完");
        }
        fis.close();
        socket.close();
    }
}
